<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.systemCode"
              placeholder="请输入名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <el-button type="primary" icon="el-icon-back" size="medium" @click="goBack()">返回</el-button>
        <perButton label="新建一级菜单" size="medium" icon="el-icon-plus" perms="PC_SYS_MENU_ADD" type="primary"
                   @click="handlerAdd"></perButton>
        <!-- <perButton label="菜单排序" size="medium" icon="el-icon-sort" perms="PC_SYS_MENU_SORT" type="warning"
                   @click="handlerSort"></perButton> -->
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%;"
        class="elTable"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children'}">
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column prop="menuCode" label="菜单编码" align="center"/>
        <el-table-column prop="menuName" label="菜单名称" align="center"/>
        <el-table-column prop="menuIcon" label="菜单选中照片" align="center">
          <template slot-scope="scope">
            <div class="d-flex justify-content-center">
              <img class="avatar2" :src="scope.row.menuIcon" v-if="scope.row.menuIcon" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="menuIcon" label="菜单未选中照片" align="center">
          <template slot-scope="scope">
            <div class="d-flex justify-content-center">
              <img class="avatar2" :src="scope.row.menuIconOff" v-if="scope.row.menuIconOff" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="menuDes" label="菜单描述" show-overflow-tooltip align="center"/>
        <el-table-column prop="createByName" label="创建人" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="360" align="center">
          <template #default="{ row }">
            <perButton label="新建子菜单" type="primary" size="mini" perms="PC_SYS_MENU_CHILD" @click="addChild(row)" v-if="row.menuLevel == 1"></perButton>
            <perButton label="配置布局" type="primary" size="mini" perms="PC_SYS_MENU_LAYOUT" @click="settingLayout(row)" v-if="row.menuLevel == 2"></perButton>
            <perButton label="配置卡片" type="primary" size="mini" perms="PC_SYS_MENU_CARD" @click="settingCard(row)" v-if="row.menuLevel == 2"></perButton>
            <perButton label="编辑" type="primary" size="mini" perms="PC_SYS_MENU_EDIT" @click="editMenu(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PC_SYS_MENU_DEL" @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新建弹框 -->
    <yc-dialog ref="addDialog" :centerTitle="centerTitle" :dialogTitle="dialogTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="addFormRef"
            :model="addForm"
            :rules="rules"
            label-width="110px"
          >
            <el-form-item label="菜单名称" prop="menuName">
              <el-input
                v-model="addForm.menuName"
              />
            </el-form-item>
            <el-form-item label="菜单等级" prop="menuLevel">
              <el-select v-model="addForm.menuLevel" disabled>
                <el-option label="一级" :value="1" />
                <el-option label="二级" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item label="菜单描述">
              <el-input
                v-model="addForm.menuDes"
                type="textarea" :rows="2"
              />
            </el-form-item>
            <el-form-item label="选中照片" prop="menuIcon" v-if="addForm.menuLevel=='2'">
              <div class="col-center" v-if="addForm.menuIcon">
                <img
                  :src="addForm.menuIcon"
                  class="avatar2 mr-3"
                />
                <el-button icon="el-icon-edit" @click="selectPic('select')">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic('select')" v-else>选择照片</el-button>
            </el-form-item>
            <el-form-item label="未选中照片" prop="menuIconOff" v-if="addForm.menuLevel=='2'">
              <div class="col-center" v-if="addForm.menuIconOff">
                <img
                  :src="addForm.menuIconOff"
                  class="avatar2 mr-3"
                />
                <el-button icon="el-icon-edit" @click="selectPic('')">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic('')" v-else>选择照片</el-button>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addLoading" @click="addSave">确定</el-button>
        <el-button @click="$refs.addDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>

    <!-- 选择照片 -->
    <yc-dialog ref="iconDialog" :centerTitle="centerTitle2" :dialogTitle="dialogTitle2">
      <template #content>
        <div class="dialog-content">
          <div class="d-flex flex-wrap">
            <div class="d-flex align-items-center flex-column mr-4 mb-4" v-for="(item ,index) in iconList" :key="index">
              <div class="picdiv mb-2">
                <img :src="item.picUrl" class="bgcss" @click="iconUrl = item.picUrl" />
              </div>
              <el-radio :label="item.picUrl" v-model="iconUrl">{{ item.picName }}</el-radio>
            </div>
          </div>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" @click="iconSave">确定</el-button>
        <el-button @click="$refs.iconDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>

    <!-- 封装操作后弹框 -->
    <global-message v-if="isGlobalMessageShow" @receive="receive" :message="message"/>
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import { queryList, insertByModel, updateByModel, deleteMenu } from "@/api/systemBuilding/menu"
  import { getBasePic } from "@/api/systemBuilding/pcSystemBuilding"

  export default {
    components: {
      ycDialog,
      perButton,
    },
    data() {
      return {
        isGlobalMessageShow: false,
        message: "",

        systemCode: "",
        tableData: [],
        loading: false,
        searchModel: {
          menuName: "",
        },

        centerTitle: "",
        dialogTitle: "",
        centerTitle2: "",
        dialogTitle2: "",
        addLoading: false,

        addForm: {
          menuName: "",
          menuLevel: 1,
          menuDes: "",
          menuIcon: "",
          menuIconOff: "",
        },
        rules: {
          menuName: [
            {required: true, message: "请输入菜单名称", trigger: "blur"}
          ],
          menuIcon: [
            {required: true, message: "请上传菜单选中照片", trigger: "blur"}
          ],
          menuIconOff: [
            {required: true, message: "请上传菜单照片", trigger: "blur"}
          ],
        },
        iconList: [],
        iconUrl: "",
        selectIcon: ""
      }
    },
    created() {
      this.systemCode = this.$route.query.systemCode;
      this.getAccountData()
      this.getBasePicList()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData() {
        this.loading = true
        const {object} = await queryList({
          systemCode: this.systemCode
        })
        this.tableData = object;
        this.loading = false
      },

      getBasePicList() {
        getBasePic({picType: 1}).then(res => {
          if(res.status == 200) {
            this.iconList = res.object;
          }
        })
      },

      goBack() {
        this.$router.go(-1)
      },

      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },

      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          systemCode: "",
        }
        this.getAccountData()
      },

      /**
       * 新建
       */
      handlerAdd() {
        this.dialogTitle = "新建菜单"
        this.centerTitle = "新建菜单"
        this.$refs.addDialog.dialogVisible = true

        Object.keys(this.addForm).forEach((item) => {
          this.addForm[item] = ""
        })
        this.addForm.menuLevel = 1;
      },

      addChild(row) {
        this.dialogTitle = "新建子菜单"
        this.centerTitle = "新建子菜单"
        this.$refs.addDialog.dialogVisible = true

        Object.keys(this.addForm).forEach((item) => {
          this.addForm[item] = ""
        })
        this.addForm.menuLevel = 2;
        this.addForm.fatherMenuCode = row.menuCode;
      },

      /**
       * 编辑
       */
      editMenu(row) {
        this.dialogTitle = "编辑菜单"
        this.centerTitle = "编辑菜单"
        this.$refs.addDialog.dialogVisible = true
        this.addForm = {
          id: row.id,
          menuName: row.menuName,
          menuIcon: "",
          menuDes: row.menuDes,
          menuLevel: parseInt(row.menuLevel)
        }
        if(row.menuLevel == 2) {
          this.addForm.menuIcon = row.menuIcon;
        }
      },

      /**
       * 新建保存
       */
      addSave() {
        this.$refs.addFormRef.validate(async (v) => {
          if(v) {
            this.addLoading = true;
            let res = null;
            if(this.dialogTitle == "新建菜单" || this.dialogTitle == "新建子菜单") {
              res = await insertByModel({...this.addForm, systemCode: this.systemCode});
            } else if(this.dialogTitle == "编辑菜单") {
              res = await updateByModel({...this.addForm});
            }

            if (res.status == 200) {
              this.getAccountData()
              this.$refs.addDialog.dialogVisible = false
              this.$message.success(res.message)
            }
            this.addLoading = false;
          }
        })
      },

      /**
       * 选择图片
       */
      selectPic(select) {
        this.dialogTitle2 = "选择图片"
        this.centerTitle2 = "选择图片"
        this.selectIcon = select
        this.iconUrl = ""
        this.$refs.iconDialog.dialogVisible = true
      },

      /**
       * 选择图片保存
       */
      iconSave() {
        if(this.selectIcon=='select') {
          this.addForm.menuIcon = this.iconUrl
        } else {
          this.addForm.menuIconOff = this.iconUrl
        }

        this.$refs.iconDialog.dialogVisible = false
      },

      /**
       * 配置布局
       */
      settingLayout(row) {
        this.$router.push({
          path: "/systemBuilding/pcSystemBuildLayout",
          query: {
            systemCode: this.systemCode,
            menuCode: row.menuCode
          }
        })
      },

      /**
       * 配置卡片
       */
      settingCard(row) {
        this.$router.push({
          path: "/systemBuilding/settingCard",
          query: {
            systemCode: this.systemCode,
            menuCode: row.menuCode
          }
        })
      },

      /**
       * 删除
       */
      handlerDelete({id}) {
        this.$confirm("确定删除此菜单吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const {message, status} = await deleteMenu({id})
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },

      /**
       * globalMessage的知道按钮的方法
       * */
      receive() {
        this.isGlobalMessageShow = false
      },
    }
  }
</script>

<style lang="scss" scoped>
  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .avatar2 {
    width: 50px;
    height: 50px;
    display: block;
  }

  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    padding: 5px 20px;
  }

  .picdiv {
    width: 160px;
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;

    .bgcss {
      max-width: 160px;
      max-height: 90px;
      cursor: pointer;
    }
  }

  .el-radio {
    display: flex;
    flex-direction: column-reverse;
    align-items: center;

    ::v-deep.el-radio__label {
      margin-bottom: 5px;
    }
  }
</style>
